home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / BufferedOutputStream.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  136 lines

  1. /*
  2.  * @(#)BufferedOutputStream.java    1.21 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * The class implements a buffered output stream. By setting up such 
  19.  * an output stream, an application can write bytes to the underlying 
  20.  * output stream without necessarily causing a call to the underlying 
  21.  * system for each byte written. The data is written into a buffer, 
  22.  * and then written to the underlying stream if the buffer reaches 
  23.  * its capacity, the buffer output stream is closed, or the buffer 
  24.  * output stream is explicity flushed. 
  25.  *
  26.  * @author  Arthur van Hoff
  27.  * @version 1.21, 07/01/98
  28.  * @since   JDK1.0
  29.  */
  30. public 
  31. class BufferedOutputStream extends FilterOutputStream {
  32.     /**
  33.      * The buffer where data is stored. 
  34.      *
  35.      * @since   JDK1.0
  36.      */
  37.     protected byte buf[];
  38.  
  39.     /**
  40.      * The number of valid bytes in the buffer. 
  41.      *
  42.      * @since   JDK1.0
  43.      */
  44.     protected int count;
  45.     
  46.     /**
  47.      * Creates a new buffered output stream to write data to the 
  48.      * specified underlying output stream with a default 512-byte buffer size.
  49.      *
  50.      * @param   out   the underlying output stream.
  51.      * @since   JDK1.0
  52.      */
  53.     public BufferedOutputStream(OutputStream out) {
  54.     this(out, 512);
  55.     }
  56.  
  57.     /**
  58.      * Creates a new buffered output stream to write data to the 
  59.      * specified underlying output stream with the specified buffer size. 
  60.      *
  61.      * @param   out    the underlying output stream.
  62.      * @param   size   the buffer size.
  63.      * @since   JDK1.0
  64.      */
  65.     public BufferedOutputStream(OutputStream out, int size) {
  66.     super(out);
  67.     buf = new byte[size];
  68.     }
  69.  
  70.     /** Flush the internal buffer */
  71.     private void flushBuffer() throws IOException {
  72.         if (count > 0) {
  73.         out.write(buf, 0, count);
  74.         count = 0;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Writes the specified byte to this buffered output stream. 
  80.      *
  81.      * @param      b   the byte to be written.
  82.      * @exception  IOException  if an I/O error occurs.
  83.      * @since      JDK1.0
  84.      */
  85.     public synchronized void write(int b) throws IOException {
  86.     if (count >= buf.length) {
  87.         flushBuffer();
  88.     }
  89.     buf[count++] = (byte)b;
  90.     }
  91.  
  92.     /**
  93.      * Writes <code>len</code> bytes from the specified byte array 
  94.      * starting at offset <code>off</code> to this buffered output stream.
  95.      *
  96.      * <p> Ordinarily this method stores bytes from the given array into this
  97.      * stream's buffer, flushing the buffer to the underlying output stream as
  98.      * needed.  If the requested length is at least as large as this stream's
  99.      * buffer, however, then this method will flush the buffer and write the
  100.      * bytes directly to the underlying output stream.  Thus redundant
  101.      * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
  102.      *
  103.      * @param      b     the data.
  104.      * @param      off   the start offset in the data.
  105.      * @param      len   the number of bytes to write.
  106.      * @exception  IOException  if an I/O error occurs.
  107.      */
  108.     public synchronized void write(byte b[], int off, int len) throws IOException {
  109.     if (len >= buf.length) {
  110.         /* If the request length exceeds the size of the output buffer,
  111.                flush the output buffer and then write the data directly.
  112.                In this way buffered streams will cascade harmlessly. */
  113.         flushBuffer();
  114.         out.write(b, off, len);
  115.         return;
  116.     }
  117.     if (len > buf.length - count) {
  118.         flushBuffer();
  119.     }
  120.     System.arraycopy(b, off, buf, count, len);
  121.     count += len;
  122.     }
  123.  
  124.     /**
  125.      * Flushes this buffered output stream. This forces any buffered 
  126.      * output bytes to be written out to the underlying output stream. 
  127.      *
  128.      * @exception  IOException  if an I/O error occurs.
  129.      * @see        java.io.FilterOutputStream#out
  130.      */
  131.     public synchronized void flush() throws IOException {
  132.         flushBuffer();
  133.     out.flush();
  134.     }
  135. }
  136.